package com.yztc.love_travel.ui.mainservice.playinlocal.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.yztc.love_travel.R;
import com.yztc.love_travel.adater.PlayinLocalAdapter;
import com.yztc.love_travel.bean.PlayinLocalBean;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.mainservice.playinlocal.Presenter.PlayinLocalPresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "玩当地"
 */
public class PlayInLocalActivity extends AppCompatActivity implements IPlayinLocalView {

    @BindView(R.id.playinlocal_toolbar)
    Toolbar playinlocalToolbar;
    @BindView(R.id.playinlocal_history)
    ListView playinlocalHistory;
    @BindView(R.id.activity_play_in_local)
    LinearLayout activityPlayInLocal;
    private PlayinLocalPresenterImpl mPresenter;
    private List<PlayinLocalBean.ManualArticleListBean> mlayinLocalBean;
    private PlayinLocalAdapter mPlayinLocalAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_in_local);
        ButterKnife.bind(this);
        initPresenter();
        loadData();
        initToolBar();
        initAdapter();
    }

    private void loadData() {
        Map<String,Object> params=new HashMap<>();
        params.put(URLConstant.PlayinLocal.PAGENO,0);
        params.put(URLConstant.PlayinLocal.PAGESIZE,20);
        params.put(URLConstant.PlayinLocal.STATYS,1);
        params.put(URLConstant.PlayinLocal.MANUALCATEID,2);
        mPresenter.getPlayinLocalBean(URLConstant.PlayinLocal.URL_PLAYINLOCAL_BASE,params);
    }

    private void initAdapter() {
        mlayinLocalBean=new ArrayList<>();
        mPlayinLocalAdapter=new PlayinLocalAdapter(mlayinLocalBean,this);
        playinlocalHistory.setAdapter(mPlayinLocalAdapter);
        playinlocalHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(PlayInLocalActivity.this, PlayinLocalDetailActivity.class);
                int idl =mlayinLocalBean.get(position).getArticleId();
                intent.putExtra(OtherConstant.KEY_ARTICAL_LOCAL_ID,idl);
                startActivity(intent);
            }
        });
    }

    private void initToolBar() {
        playinlocalToolbar.setTitle(R.string.play_in_local);
        setSupportActionBar(playinlocalToolbar);
        ActionBar action=getSupportActionBar();
        action.setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initPresenter() {
        mPresenter=new PlayinLocalPresenterImpl(this);
    }

    @Override
    public void loadStart() {

    }

    @Override
    public void loadFinish() {

    }

    @Override
    public void loadError() {

    }

    @Override
    public void showPlayinLocalData(PlayinLocalBean playinlocalBean) {
        List<PlayinLocalBean.ManualArticleListBean> manualArticleList = playinlocalBean.getManualArticleList();
        mlayinLocalBean.addAll(manualArticleList);
        mPlayinLocalAdapter.notifyDataSetChanged();
    }
}
